package com.cloudptt.api.product.func.call.group.fsm.call;

import android.content.Context;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.log.Log;


public class WattingState implements ICallState,IRollback
{
	private GroupCallAgent agent = null;
	private RollbackTimer rollbackTimer = null;
	private int callDir = CallAttr.CallDir_MO;

	public WattingState(Context ctx, GroupCallAgent agent,int sec, int callDir)
	{
		this.agent = agent;
		this.rollbackTimer = new RollbackTimer(ctx,sec,this);
		this.callDir = callDir;
	}
	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Reject == btnAction)
		{
			int result = this.agent.doHangupAction(getErrCode());
			if(CauseCont.OperateCheck_Suc == result)
			{
				this.agent.goToState(CallConst.CallState_DisconnectingState);
			}
			return result;
		}
		else if(CallConst.BtnAction_Accept == btnAction)
		{
			this.agent.goToState(CallConst.CallState_ConnectingState);
			return CauseCont.OperateCheck_Suc;
		}
		else if(CallConst.BtnAction_Ring == btnAction)
		{
			int result = this.agent.ring();
			return result;
		}
		return CauseCont.OperateErr_Invalid;
	}
	public void receiveCallAction(int talkAction)
	{
		if(CallConst.Session_Confirmed_Active == talkAction)
		{
//			gotoActiviteState();
			this.agent.goToState(CallConst.CallState_Activite);
		}
		else if(CallConst.Session_Disconnected == talkAction)
		{
//			gotoInitialState();
			this.agent.goToState(CallConst.CallState_InitState);
		}
	}


	public int getState()
	{
		return CallConst.CallState_WattingState;
	}
	public void destory()
	{
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}
	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(GroupCallAgent.Log_Key, "CallingState rollbackState");
		if(agent != null)
		{
			int result = this.agent.doHangupAction(408);
			if(CauseCont.OperateCheck_Suc == result)
			{
//				gotoDisconnectingState();
				this.agent.goToState(CallConst.CallState_DisconnectingState);
			}
		}
	}
	private int getErrCode()
	{
		int code = 486;
		if(callDir == CallAttr.CallDir_MO)
		{
			code = 487;
		}
		return code;
	}
}
